package com.feel.mall.goodsservice.service;

import com.feel.mall.common.bean.Comment;
import com.feel.mall.common.bean.User;
import com.feel.mall.common.exception.FeelException;
import com.feel.mall.common.service.CommentService;
import com.feel.mall.common.service.GoodsService;
import com.feel.mall.common.service.UserService;
import com.feel.mall.goodsservice.mapper.CommentMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 刘永钦
 * @Description:
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private GoodsService goodsService;
    @DubboReference
    private UserService userService;

    @Override
    public Boolean addInfo(Comment comment) {
        Date now = new Date();
        comment.setUpdateTime(now);
        comment.setCreateTime(now);

        if (comment.getScore() == null) {
            comment.setScore(0f);
        }
        Integer i = commentMapper.addInfo(comment);
        return i >= 1;
    }

    @Override
    public Boolean dellInfo(Integer commentId, Integer userId) throws FeelException {
        Comment comment = commentMapper.getById(commentId);
        if (comment == null) {
            throw new FeelException("评论不存在，请重试");
        }
        if (!comment.getUserId().equals(userId)) {
            throw new FeelException("权限错误，请重试");
        }
        // 执行删除
        Integer i = commentMapper.delInfo(commentId);
        return i >= 1;
    }

    @Override
    public List<Comment> getInfo(Comment condition) {
        List<Comment> list = commentMapper.getByCondition(condition);
        list = list.stream()
                .filter(item -> item.getType().equals(0))
                .collect(Collectors.toList());
        list.forEach(comment -> {
            User user = userService.selectById(comment.getUserId());
            Comment comment1 = new Comment();
            comment1.setType(1);
            comment1.setParentId(comment.getId());
            List<Comment> child = commentMapper.getByCondition(comment1);
            child.forEach(child1 -> {
                User user1 = userService.selectById(child1.getUserId());
                child1.setUser(user1);
            });

            comment.setComment(child);
            comment.setUser(user);
        });
        return list;
    }

    @Override
    public Boolean updateInfo(Comment comment, Integer userId) throws FeelException {
        Comment localComment = commentMapper.getById(comment.getId());
        if (localComment == null) {
            throw new FeelException("评论不存在");
        }
        if (!localComment.getUserId().equals(userId)) {
            throw new FeelException("权限错误");
        }
        Date date = new Date();
        comment.setUpdateTime(date);
        Integer i = commentMapper.updateInfo(comment);
        return i >= 1;
    }
}
